var App = require('app');

App.UserSignonController = Em.ObjectController.extend({
	signon: function() {
		var me = this;
		var p = $('#signonForm').validate().form();
		if (!p) return;
		
		$("#signonForm").ajaxSubmit(function(json) {
			if (json.success) {
				if (Util.isNotEmpty(json.token)) {
					$.cookie(Vari.TokenName, json.token);
				} else {
					$.removeCookie(Vari.TokenName);
				}
				
				Vari.UserEmail = json.email;
				me.transitionToRoute("apps");
			} else {
				$.removeCookie(Vari.TokenName);
			}
	    });
	    return false;
	},
	guestSignon: function() {
		var me = this;
    	$.ajax({
			async : false,
			url : Vari.ApiRestV1 + 'user/signon',
			data : {
				email: "guest@palm.io",
				password: "pass",
				rememberMe: "on"
			},

			success : function(json) {
				if (json.success) {
					if (Util.isNotEmpty(json.token)) {
						$.cookie(Vari.TokenName, json.token);
					} else {
						$.removeCookie(Vari.TokenName);
					}					
					Vari.UserEmail = json.email;
					me.transitionToRoute("apps");
				} else {
					$.removeCookie(Vari.TokenName);
				}
			}
		});
	    return false;
	}
});